<?php

namespace app\index\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use think\Session;
use think\Validate;
use tree\Tree;

class Index extends Controller
{
    public function userDetail()
    {
        $input = Request()->param();

        $user_id = $input['user_id'];
        $follow_user_id = $input['follow_user_id'] ? : 0;// 被查看人的用户id

        if (empty($user_id) && empty($follow_user_id)) apiReturn(2, '用户ID缺失');


        $err_msg = '';
        if (empty($user_id)) $err_msg = '您还未登录，请先登录';

        // 查看该用户已开通的最高等级会员
        $user_service_info = Db::name('user_service')->where(['user_id' => $user_id, 'expire_time' => ['gt', time()]])->order('price desc')->find();
        if (empty($user_service_info)) {
            $err_msg = '您还未开通服务';
        } else {
            $is_look = Db::name('user_look')->where(['user_id' => $user_id, 'to_user_id' => $follow_user_id, 'is_valid' => 1, 'is_free' => 0])->count();
            if($is_look == 0){
                // 查看今天已经查看了几个用户的信息
                $today_start = strtotime(date('Y-m-d'));
                $today_end = strtotime(date('Y-m-d 23:59:59'));
                $look_count = Db::name('user_look')->where(['user_id' => $user_id, 'is_free' => 0, 'create_time' => ['between', [$today_start, $today_end]]])->count();
                if($look_count >= $user_service_info['look_count']) $err_msg = '您今日查看用户资料已达上限';
            }
        }

        $this->assign('err_msg', false);

        $where_user_id = $follow_user_id ? : $user_id;

        $info = Db::name('user u')->join('user_details d', 'u.id = d.user_id', 'left')
            ->field('u.avatar,d.username,u.id_num,u.status,u.wechat,u.qq,u.mobile,d.*')
            ->where(['u.id' => $where_user_id])
            ->find();

        if ($info) {
            if ($info['status'] == 2) apiReturn(2, '您的账号已被禁用，请联系管理员');

            // 记录用户之间的查看记录
            if($err_msg == '' && $is_look == 0){
                $user_look_data = [
                    'user_id' => $user_id,
                    'to_user_id' => $follow_user_id,
                    'is_valid' => 1,
                    'create_time' => time()
                ];
                Db::name('user_look')->insert($user_look_data);
            }

            $info['avatar_path'] = doAvatar($info['avatar']);
            $info['photos_arr'] = doImgs($info['photos']);
            $info['birth_year'] = $info['birth_date'] && $info['birth_date'] != '0000-00-00' ? explode('-', $info['birth_date'])[0] . '年' : '';

            // 关注我的数量
            $info['follow_me_count'] = Db::name('follow')->where(['follow_user_id' => $where_user_id, 'is_follow' => 1])->count();
            // 我关注的数量
            $info['me_follow_count'] = Db::name('follow')->where(['user_id' => $where_user_id, 'is_follow' => 1])->count();

            if ($follow_user_id > 0 && $user_id > 0 && $user_id != $follow_user_id) {
                $is_follow = Db::name('follow')->where(['user_id' => $user_id, 'follow_user_id' => $follow_user_id])->value('is_follow');
                $info['is_follow'] = $is_follow ? 1 : 0;
            } else {
                $info['is_follow'] = -1;
            }
        } else {
            apiReturn(2, '用户数据不存在');
        }

        $auto_content = Db::name('auth_article')->where(['type' => 1])->value('content');
        $auto_content = doEditorContent($auto_content);

        $this->assign('info', $info);
        $this->assign('auto_content', $auto_content);

        return $this->fetch();
    }
}
